using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using WhsFacebook.Whs.Contracts;

namespace WhsFacebook.Whs.WhsAddin.SettingsControls
{
    public partial class ServiceNotRunning : SettingsControlBase
    {
        public ServiceNotRunning(WhsFacebookSettingsTab tab)
            : base(tab)
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Initialization start");

            try
            {
                InitializeComponent();

                // done
                if (_log.IsDebugEnabled)
                    _log.Debug("Initialization done");
            }
            catch (Exception e)
            {
                _log.Fatal("Failed to initialize", e);

                throw;
            }
        }

        private void startServiceLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            StartWindowsService();
        }

        private void startServiceBgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                using (new UserRecognizeWait(false))
                {
                    switch (_singletonService.IsWindowsServiceRunning())
                    {
                        case SingletonServiceFactory.ServiceStatus.Running:
                            // everything is fine
                            break;

                        case SingletonServiceFactory.ServiceStatus.NotInstalled:
                            return;

                        case SingletonServiceFactory.ServiceStatus.Stopped:
                            _singletonService.StartWindowsService();
                            break;

                        default:
                            throw new Exception("unknown value");
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Error("Failed to start Windows Home Server on Facebook service", ex);

                Microsoft.HomeServer.Controls.QMessageBox.Show(
                    ex.Message,
                    Properties.Resources.txtServiceStartFailed,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }

        private void startServiceBgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            switch (_singletonService.IsWindowsServiceRunning())
            {
                case SingletonServiceFactory.ServiceStatus.Running:
                    // everything is fine
                    AddinStateManager.Instance.SetState(AddinState.Ready);
                    break;

                case SingletonServiceFactory.ServiceStatus.NotInstalled:
                    this.statusImage.Image = Properties.Resources.imgError;
                    this.serviceStatusLabel.Text = Properties.Resources.txtServiceStatusNotInstalled;
                    this.startServiceLink.Visible = false;
                    this.startServiceLink.Enabled = false;
                    return;

                case SingletonServiceFactory.ServiceStatus.Stopped:
                    this.statusImage.Image = Properties.Resources.imgError;
                    this.serviceStatusLabel.Text = Properties.Resources.txtServiceStatusStopped;
                    this.startServiceLink.Visible = true;
                    this.startServiceLink.Enabled = true;
                    break;

                default:
                    throw new Exception("unknown value");
            }
        }

        private void StartWindowsService()
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Starting Windows Home Server on Facebook singleton service...");

            // start service
            this.statusImage.Image = Properties.Resources.imgProgressAnim;
            this.startServiceLink.Enabled = false;
            this.serviceStatusLabel.Text = Properties.Resources.txtServiceStatusStarting;

            this.startServiceBgWorker.RunWorkerAsync();
        }

        private void ServiceNotRunning_Load(object sender, EventArgs e)
        {
            StartWindowsService();
        }
    }
}